// const Koa = require('koa');
import Koa from 'koa';
const app = new Koa();
import { Buffer } from 'buffer';
import fs from 'fs';

app.use(async (ctx, next)=> {
    ctx.set('Access-Control-Allow-Origin', '*');
    ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    if (ctx.method == 'OPTIONS') {
      ctx.body = 200; 
    } else {
      await next();
    }
});
// response
app.use(async ctx => {
    console.log(ctx.method)
    ctx.req.addListener('data', (postDataChunk) => {
        // const json = postDataChunk.toJSON();
        console.log('收到数据 ---->', JSON.parse(postDataChunk), JSON.parse(Buffer.from(postDataChunk.toJSON())))
        console.log('拼接数据' ,Buffer.concat([Buffer.from('export default '), postDataChunk]));
        fs.writeFileSync('./dsl/test.js', Buffer.concat([Buffer.from('export default '), postDataChunk]));
    })

    ctx.req.addListener('end', (postDataChunk) => {
        
    })
    ctx.body = '';
});

app.listen(8000);